"use strict";
// https://gitee.com/easyatgitee/common_eggjs_app/blob/master/app/controller/sign.js
const Controller = require('./base');
const crypto = require("crypto");
class ArticleController extends Controller {
 
  async search(status) {
    let { ctx, app } = this; 
    let { page,pageSize, title,type } = ctx.request.body; 
    page = page || 1; pageSize = pageSize  || 10;
    var offset = 0;
    var offset = (page - 1) * pageSize 

    let  sql = `select count (id) as count from  article where 1 = 1 `
    if(title)  sql +=` and title like %${title}%`;
    if(type) sql +=` and type = ${type} `;  
    if(status)  sql +=` and status = 1 `;  
    let data = await this.app.mysql.query(sql);
    let count = data[0].count; 

    sql = `select id,title,tip, actionTime,video,type,videoImg from  article where 1 = 1 `
    if(title)  sql +=` and title like %${title}%`;
    if(type) sql +=` and type = ${type} `;
    if(status)  sql +=` and status =  ${status} `;  
    sql +=` limit ${offset} , ${pageSize} ` 
    
    data = await this.app.mysql.query(sql); 
 
    return { count,data }
   
  }
  async list() {
    let { ctx, app } = this;  
    var data = await this.search()
    ctx.apiSuccess(data);
  }
  async appList() { 
    let { ctx, app } = this;  
    var data = await this.search(1)
    ctx.apiSuccess(data);
  }

  async detail() {
    let { ctx, app } = this; 
    let { id } = ctx.request.body; 
    
    const article = await this.app.mysql.get('article', {   id:id,   }); 
    const comment = await app.mysql.select('comment',{
      where: { tid: id }, 
      limit: 10,
      offset: 0
    });
     
    ctx.apiSuccess({ article,comment, });
  }

  async article() {
    let { ctx, app } = this; 
    let { title,tip, content,actionTime,video,type,videoImg } = ctx.request.body;
    console.log(ctx.request.body)
    let res = await this.app.mysql.insert('article',{ 
      title, tip, content, actionTime, video, type, videoImg, 
      created_time:this.formatDate(Date.now(),"YYYY-MM-DD HH:mm:ss"),
    })

    ctx.apiSuccess(res);
  } 

  async update() {
    let { ctx, app } = this;  
    let article = this.delProperty(ctx.request.body); // 或者传了就是去修改
   
    console.log(article)
    const data = await this.app.mysql.update('article',article,{ where:{ id:article.id }})
    ctx.apiSuccess(data);
  }  
 
}

module.exports = ArticleController;
/*
CREATE TABLE `article`  (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `title` varchar(100),
  `content` varchar(3000),
  `actionTime` varchar(100),
  `video` varchar(200), 
  `videoImg` varchar(200),
  `type` int(11) DEFAULT NULL,
  `status` int(11) DEFAULT 0,
  `created_time` datetime(0) DEFAULT NULL,  
  PRIMARY KEY (`id`) USING BTREE
);

CRUD 
const results = await app.mysql.select('sign',{
  where: { uid: id },
  orders: [['created_at','desc'], ['id','desc']],
  limit: 10,
  offset: 0
});
const record = await this.app.mysql.get('sign', {  
  uid:id, 
  created_at:this.formatDate(Date.now(),"YYYY-MM-DD"),
});  
if(record)  return  this.success(record) 
let data = await this.app.mysql.insert('sign',{ 
  uid:id, 
  created_at:this.formatDate(Date.now(),"YYYY-MM-DD"),// "2020-10-10 10:10:10" 
})
*/

